package org.ingeniart.siec.controller;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import org.ingeniart.siec.service.AbstracRegistration;

public abstract class AbstractController<T> {

	@Inject
	private FacesContext facesContext;
	
	private Class<T> modelClass;
	
	protected abstract AbstracRegistration<T> getRegistration();
	public abstract T getObject();
	public abstract void setObject(T obClass);
	public AbstractController() {
		// TODO Auto-generated constructor stub
	}
	public AbstractController(Class<T> model) {
		// TODO Auto-generated constructor stub
		this.modelClass=model;
	}
		
	public void register() throws Exception
	{
		getRegistration().register(getObject());
		facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Registrada","Inscripcion realizada con Exito!"));
		initNewObject();
	}
	
	@PostConstruct
	public void initNewObject() throws InstantiationException, IllegalAccessException
	{
		setObject(modelClass.newInstance());		
	}
	
	public SelectItem[] getSelectedItems()
	{
		return getRegistration().getSelectItems(false);
	}
	
	
}
